/*   Copyright (C) 2011 Wolfgang Hankeln, Norma J. Wendel
 *
 *   This file is part of CDinFusion.
 *
 *   CDinFusion is free software: you can redistribute it and/or
 *   modify it under the terms of the GNU Lesser General Public 
 *   License in version 3 (LGPL3) as published by the Free Software
 *   Foundation version 3 of the License.
 *
 *   CDinFusion is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *   Lesser General Public License for more details.
 *
 *   You should have received a copy of the Lesser General Public
 *   License along with CDinFusion; see the file COPYING. If not,
 *   write to the Free Software Foundation, 59 Temple Place - Suite 330,
 *   Boston, MA 02111-1307, USA or see <http://www.gnu.org/licenses/>.
 *   
 *   If you have any questions, send an email to: megx@mpi-bremen.de.
 */
package org.cdinfusion.web.controller;

import java.io.FileInputStream;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

public class End_Controller extends AbstractController {
	public ModelAndView handleRequestInternal(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		System.out.println("##### End_Controller #####");
		request.setCharacterEncoding("UTF-8");

		HttpSession session = request.getSession();
		String button = request.getParameter("submit");
		String catalinahome = System.getenv("CATALINA_HOME");
		String path = "";
		String filename = "";
		String sessionID = request.getSession().getId();
		String opt_2b = (String) session.getAttribute("opt_2b");

		if (button.contains("FASTAFile")) {
			path = catalinahome + "/webapps/CDinFusion/data/cdfile_"
					+ sessionID + ".fasta";
			filename = "cdfile_" + sessionID + ".fasta";
			response.setContentType("text/html; charset=UTF-8");
		} else if (button.contains("StructuredCommentFile")) {
			if (opt_2b.equals("one")) {
				path = catalinahome + "/webapps/CDinFusion/data/sequin_0_"
						+ sessionID + ".cmt";
				filename = "sequin_0_" + sessionID + ".cmt";
				response.setContentType("text/html; charset=UTF-8");
			} else if (opt_2b.equals("many")) {
				path = catalinahome + "/webapps/CDinFusion/data/cmt_files_"
						+ sessionID + ".zip";
				filename = "cmt_files_" + sessionID + ".zip";
				response.setContentType("application/zip");
			}
		}
		final StringBuilder header = new StringBuilder("attachment; Filename=");
		header.append(filename);
		response.setHeader("Content-Disposition", header.toString());

		FileInputStream fileInputStream = null;
		try {
			fileInputStream = new FileInputStream(path);
			IOUtils.copy(fileInputStream, response.getOutputStream());
			response.flushBuffer();
		} catch (IOException e) {
			// TODO ???
		} finally {
			IOUtils.closeQuietly(fileInputStream);
		}
		return null;
	}
}